#include <stdlib.h>
#include <string>

#include "include/ModelSVG.h"
#include "include/Model3D.h"
#include "include/Territoire.h"
#include "include/LoadModelVisitor.h"

#define DEBUG 0

using namespace std;

void subpath(const char cmd, const char *coords, Shape * _shape)
{
   
   float x=0, y=0, x2=0, x3=0, y2=0, y3=0;
   
   switch(cmd)
   {
      case 'M':
         sscanf(coords, "%f,%f", &x, &y);
         _shape->addPoint(x, y);
         if(DEBUG)
            printf("coords [%s] -> X : %.3f Y: %.3f\n", coords, x, y);
         break;
      case 'L':
         sscanf(coords, "%f,%f", &x, &y); 
         _shape->addPoint(x, y);
         if(DEBUG)
            printf("coords [%s] -> X : %.3f Y: %.3f\n", coords, x, y);
         break;
      case 'C':
         sscanf(coords, "%f,%f %f,%f %f,%f", &x, &y, &x2, &y2, &x3, &y3);
         //_shape->addPoint( Point3D(x, y) );
          _shape->addPoint(x2, y2);
         //_shape->addPoint( Point3D(x3, y3) );
          if(DEBUG)
             printf("coords: [%s] -> X : %.3f Y: %.3f | ",coords, x2, y2);
         break;
      case 'S':
         break;
         
   };
}

void ModelSVG::computeShape(const char * _definition, Territoire * _territoire)
{
   int i=0;
   int i_cmd=0;
   
   string s_path(_definition);
   int taille = strlen(_definition);

   while( i < taille )
   {
      if(_definition[i]>= 'A' && _definition[i]<='Z')
      {
         i_cmd = i;
         i++;
         while( !(_definition[i]>= 'A' && _definition[i]<='Z'))
            i++;
         subpath(_definition[i_cmd], s_path.substr(i_cmd+1,i-i_cmd-1).c_str(), _territoire->getModel3D()->getShape());
      }
      else
      {
         i++;
      }
   }
}

void ModelSVG::computeColor(const char * _definition, Territoire * _territoire)
{
   char couleur[7];
   sscanf(_definition, "fill:#%[a-f0-9];", couleur);

   char r[3];
   char v[3];
   char b[3];
   strcpy(r, string(couleur).substr(0, 2).c_str()); r[2] = 0;
   strcpy(v, string(couleur).substr(2, 2).c_str()); v[2] = 0;
   strcpy(b, string(couleur).substr(4, 2).c_str()); b[2] = 0;   

   _territoire->setCouleur((float)strtol(r, (char **)NULL, 16) / 255,
                           (float)strtol(v, (char **)NULL, 16) / 255,
                           (float)strtol(b, (char **)NULL, 16) / 255);
}

void ModelSVG::computeBorders(const char * _definition, Territoire * _territoire)
{
   int i = 0;
   int i2 = 0;
   int taille = strlen(_definition);

   while( i < taille )
   {
      if( _definition[i] != ' ' )
      {
         if( _definition[i] == ',' )
         {
            _territoire->addBorderByName( LoadModelVisitor::replace('_', ' ', string(_definition).substr(i2, i-i2)).c_str() );
            i2=i+1;
         }
       
      }
      else
         i2++;
      i++;
   }
}

void ModelSVG::computeCenter(const char * _definition, Territoire * _territoire)
{
   float x, y;
   sscanf(_definition, "%f,%f", &x, &y);
   _territoire->getModel3D()->getCenter()->setX(x);
   _territoire->getModel3D()->getCenter()->setY(y);
}
